﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WFApp2
{
    public partial class Form1 : Form
    {
        public static String num1="";
        public static String num2="";
        public static bool hasNum1=false;
        public static bool hasNum2=false;
        public static int opre=-1;
        public static double resultNum;
        public Form1()
        {
            InitializeComponent();
            result.Text = "";
            
        }

        private void bt1_Click(object sender, EventArgs e)
        {
            result.Text += bt1.Text;
            if (opre == -1)
            {
                num1 += bt1.Text;
                hasNum1 = true;
            }
            else
            {
                num2 += bt1.Text;
                hasNum2 = true;
            }
        }

        private void bt2_Click(object sender, EventArgs e)
        {
            result.Text += bt2.Text; 
            if (opre == -1)
            {
                num1 += bt2.Text;
                hasNum1 = true;
            }
            else
            {
                num2 += bt2.Text;
                hasNum2 = true;
            }
        }

        private void bt3_Click(object sender, EventArgs e)
        {
            result.Text += bt3.Text;
            if (opre == -1)
            {
                num1 += bt3.Text;
                hasNum1 = true;
            }
            else
            {
                num2 += bt3.Text;
                hasNum2 = true;
            }
        }

        private void bt4_Click(object sender, EventArgs e)
        {
            result.Text += bt4.Text;
            if (opre == -1)
            {
                num1 += bt4.Text;
                hasNum1 = true;
            }
            else
            {
                num2 += bt4.Text;
                hasNum2 = true;
            }
        }

        private void bt5_Click(object sender, EventArgs e)
        {
            result.Text += bt5.Text;
            if (opre == -1)
            {
                num1 += bt5.Text;
                hasNum1 = true;
            }
            else
            {
                num2 += bt5.Text;
                hasNum2 = true;
            }
        }

        private void bt6_Click(object sender, EventArgs e)
        {
            result.Text += bt6.Text;
            if (opre == -1)
            {
                num1 += bt6.Text;
                hasNum1 = true;
            }
            else
            {
                num2 += bt6.Text;
                hasNum2 = true;
            }
        }

        private void bt7_Click(object sender, EventArgs e)
        {
            result.Text += bt7.Text;
            if (opre == -1)
            {
                num1 += bt7.Text;
                hasNum1 = true;
            }
            else
            {
                num2 += bt7.Text;
                hasNum2 = true;
            }
        }

        private void bt8_Click(object sender, EventArgs e)
        {
            result.Text += bt8.Text;
            if (opre == -1)
            {
                num1 += bt8.Text;
                hasNum1 = true;
            }
            else
            {
                num2 += bt8.Text;
                hasNum2 = true;
            }
        }

        private void bt9_Click(object sender, EventArgs e)
        {
            result.Text += bt9.Text;
            if (opre == -1)
            {
                num1 += bt9.Text;
                hasNum1 = true;
            }
            else
            {
                num2 += bt9.Text;
                hasNum2 = true;
            }
        }

        private void bt0_Click(object sender, EventArgs e)
        {
            result.Text += bt0.Text;
            if (opre == -1)
            {
                num1 += bt0.Text;
                hasNum1 = true;
            }
            else
            {
                num2 += bt0.Text;
                hasNum2 = true;
            }
        }

        private void btadd_Click(object sender, EventArgs e)
        {            
            if (opre != -1)
            {
                btequal_Click(sender, e);
                num1 = resultNum.ToString();
                hasNum1 = true;
                opre = 0;
                result.Text = resultNum + btadd.Text;
            }else if (hasNum1)
            {
                opre = 0;
                result.Text += btadd.Text;
            }
            else if (!result.Text.Equals(""))
            {
                num1 = resultNum.ToString();
                hasNum1 = true;
                opre = 0;
                result.Text = resultNum + btadd.Text;
            }
        }

        private void btjian_Click(object sender, EventArgs e)
        {

            if (opre != -1)
            {
                btequal_Click(sender, e);
                num1 = resultNum.ToString();
                hasNum1 = true;
                opre = 1;
                result.Text = resultNum + btjian.Text;
            }else if (hasNum1)
            {
                opre = 1;
                result.Text += btjian.Text;
            }
            else if (!result.Text.Equals(""))
            {
                num1 = resultNum.ToString();
                hasNum1 = true;
                opre = 1;
                result.Text = resultNum + btjian.Text;
            }
        }

        private void btm_Click(object sender, EventArgs e)
        {if (opre != -1)
            {
                btequal_Click(sender, e);
                num1 = resultNum.ToString();
                hasNum1 = true;
                opre = 2;
                result.Text = resultNum + btm.Text;
            }else if(hasNum1)
            {
                opre = 2;
                result.Text += btm.Text;
            }
            else if (!result.Text.Equals(""))
            {
                num1 = resultNum.ToString();
                hasNum1 = true;
                opre = 2;
                result.Text = resultNum + btm.Text;
            }
        }

        private void btd_Click(object sender, EventArgs e)
        {
            if (opre != -1)
            {
                btequal_Click(sender, e);
                num1 = resultNum.ToString();
                hasNum1 = true;
                opre = 3;
                result.Text = resultNum + btd.Text;
            }else if (hasNum1)
            {
                opre = 3;
                result.Text += btd.Text;
            }else if (!result.Text.Equals(""))
            {
                num1 = resultNum.ToString();
                hasNum1 = true;
                opre = 3;
                result.Text = resultNum + btd.Text;
            }
            
        }

        private void btequal_Click(object sender, EventArgs e)
        {
            try
            {
                double opre1 = double.Parse(num1);
                double opre2 = double.Parse(num2);
                switch (opre)
                {
                    case 0:
                        resultNum = opre1 + opre2;
                        result.Text+=btequal.Text+resultNum.ToString();
                        break;
                    case 1:
                        resultNum = opre1 - opre2;
                        result.Text += btequal.Text + resultNum.ToString();
                        break;
                    case 2:
                        resultNum = opre1 * opre2;
                        result.Text+= btequal.Text + resultNum.ToString();
                        break;
                    case 3:
                        if (opre2 == 0)
                        {
                            result.Text = "null";
                        }
                        else
                        {                        
                            resultNum = opre1 / opre2;
                            result.Text += btequal.Text + Convert.ToDouble(resultNum).ToString("0.000"); 

                        }

                        break;
                }
                opre = -1;
                num1 = num2 = "";
                hasNum1 = hasNum2 = false;
            }
            catch(Exception ex)
            {
                throw ex;
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void btc_Click(object sender, EventArgs e)
        {
            opre = -1;
            num1 = num2 = "";
            hasNum1 = hasNum2 = false;
            result.Text = "";
        }
    }
}
